﻿#include "precomp.h"
#include "jconcurrent_global.h"
#include <QCoreApplication>
#include <stdlib.h>
#include <stdio.h>
#include "jthreadpool_global.h"

// - class JConcurrentCorePrivate -

/**
 * @brief The JConcurrentCorePrivate class
 */
class JConcurrentCorePrivate
{
    friend class JConcurrentCore;
public:
    /**
     * @brief JConcurrentCorePrivate
     */
    JConcurrentCorePrivate()
    {
        if (qApp == 0) {
            int argc = 0;
            (void)new QCoreApplication(argc, 0);
        }
    }

    ~JConcurrentCorePrivate()
    {
        delete qApp;
    }

private:
    static JConcurrentCore *_inst;
};

/**
 * @brief JConcurrentCorePrivate::_inst
 */
JConcurrentCore *JConcurrentCorePrivate::_inst = 0;

// - class JConcurrentCore -

/**
 * @brief JConcurrentCore::instance
 * @return
 */
JConcurrentCore *JConcurrentCore::instance()
{
    if (JConcurrentCorePrivate::_inst == 0) {
        JConcurrentCorePrivate::_inst = new JConcurrentCore();
    }

    return JConcurrentCorePrivate::_inst;
}

/**
 * @brief JConcurrentCore::releaseInstance
 */
void JConcurrentCore::releaseInstance()
{
    if (JConcurrentCorePrivate::_inst != 0) {
        delete JConcurrentCorePrivate::_inst;
        JConcurrentCorePrivate::_inst = 0;
    }
}

/**
 * @brief JConcurrentCore::run
 * @return
 */
int JConcurrentCore::run()
{
    if (qApp) {
        //
    }

    return 0;
}

/**
 * @brief JConcurrentCore::exec
 * @return
 */
int JConcurrentCore::exec()
{
    if (!qApp) {
        return -1;
    }

    return qApp->exec();
}

/**
 * @brief JConcurrentCore::JConcurrentCore
 */
JConcurrentCore::JConcurrentCore()
{
    d = new JConcurrentCorePrivate();
}

/**
 * @brief JConcurrentCore::~JConcurrentCore
 */
JConcurrentCore::~JConcurrentCore()
{
    delete d;
}
